import time

from common.Utils import FSDriver
from selenium.webdriver.common.by import By


# 私信
class Messagepage:
    url = ""
    driver = ""

    def __init__(self):
        self.url = "http://127.0.0.1:58080/index.html"
        self.driver = FSDriver.driver
        self.driver.get(self.url)
        self.driver.implicitly_wait(5)

    # 私信回复
    def messagereply(self):
        # 点击铃铛
        self.driver.find_element(By.CSS_SELECTOR,
                                 "body > div.page > header.navbar.navbar-expand-md.navbar-light.d-print-none > div > div > div:nth-child(2) > div > a > svg").click()
        self.driver.find_element(By.CSS_SELECTOR, '#index_message_offcanvasEnd > div.offcanvas-header')

        # 查看私信
        self.driver.find_element(By.CSS_SELECTOR,
                                 '#index_div_message_list > div:nth-child(1) > div > div:nth-child(3) > a > svg').click()
        # 点击回复
        self.driver.find_element(By.CSS_SELECTOR, '#btn_index_message_reply').click()
        # 回复私信
        time.sleep(1)
        self.driver.find_element(By.CSS_SELECTOR, '#index_message_reply_receive_content').send_keys('宝贝，嘴一个！')
        self.driver.find_element(By.CSS_SELECTOR, '#btn_index_send_message_reply').click()
        self.driver.find_element(By.CSS_SELECTOR, 'body > div.jq-toast-wrap.bottom-right > div')
        FSDriver.getscreeshot()

        # 回复消息后提示文本应该为‘已回复’
        assert self.driver.find_element(By.CSS_SELECTOR,
                                        '#index_div_message_list > div:nth-child(1) > div > div.col.text-truncate > a > span.index_message_item_statue').text == '[已回复]'
        time.sleep(1)
        # 关闭私信窗
        self.driver.find_element(By.CSS_SELECTOR, '#index_message_offcanvasEnd > div.offcanvas-header > button').click()

    # 私信查看
    def messageview(self):
        # 点击铃铛
        self.driver.find_element(By.CSS_SELECTOR,
                                 "body > div.page > header.navbar.navbar-expand-md.navbar-light.d-print-none > div > div > div:nth-child(2) > div > a > svg").click()
        self.driver.find_element(By.CSS_SELECTOR, '#index_message_offcanvasEnd > div.offcanvas-header')

        # 查看私信
        self.driver.find_element(By.CSS_SELECTOR,
                                 '#index_div_message_list > div:nth-child(1) > div > div.col.text-truncate > a').click()
        print(self.driver.find_element(By.CSS_SELECTOR, '#index_message_detail_title').text)
        print(self.driver.find_element(By.CSS_SELECTOR, '#index_message_detail_content').text)
        time.sleep(1)

        # 点击×
        self.driver.find_element(By.CSS_SELECTOR, '#index_message_reply_modal > div > div > div.modal-header > button').click()

        # 点击取消
        self.driver.find_element(By.CSS_SELECTOR,
                                 '#index_div_message_list > div:nth-child(1) > div > div.col.text-truncate > a').click()
        self.driver.find_element(By.CSS_SELECTOR, '#index_message_reply_cancel').click()
        FSDriver.getscreeshot()

        # 查看消息后提示文本应该为‘已读’
        # assert self.driver.find_element(By.CSS_SELECTOR,
        #                                 '#index_div_message_list > div:nth-child(1) > div > div.col.text-truncate > a > span.index_message_item_statue').text == '[已读]'
        time.sleep(1)
        self.driver.find_element(By.CSS_SELECTOR,
                                 '#index_message_reply_modal > div > div > div.modal-header > button').click()
        # 关闭私信窗
        self.driver.find_element(By.CSS_SELECTOR, '#index_message_offcanvasEnd > div.offcanvas-header > button').click()

    # 回复空信息
    def messagenull(self):
        # 点击铃铛
        self.driver.find_element(By.CSS_SELECTOR,
                                 "body > div.page > header.navbar.navbar-expand-md.navbar-light.d-print-none > div > div > div:nth-child(2) > div > a > svg").click()
        self.driver.find_element(By.CSS_SELECTOR, '#index_message_offcanvasEnd > div.offcanvas-header')

        # 查看私信
        self.driver.find_element(By.CSS_SELECTOR,
                                 '#index_div_message_list > div:nth-child(1) > div > div.col.text-truncate > a').click()
        self.driver.find_element(By.CSS_SELECTOR, '#btn_index_message_reply').click()
        self.driver.find_element(By.CSS_SELECTOR, '#btn_index_send_message_reply').click()
        FSDriver.getscreeshot()
        assert self.driver.find_element(By.CSS_SELECTOR, 'body > div.jq-toast-wrap.bottom-right > div > h2')
        time.sleep(1)
        self.driver.find_element(By.CSS_SELECTOR,
                                 '#index_message_reply_modal > div > div > div.modal-header > button').click()
        # 关闭私信窗
        self.driver.find_element(By.CSS_SELECTOR, '#index_message_offcanvasEnd > div.offcanvas-header > button').click()

    def message_test(self):
        self.messageview()
        self.messagenull()
        self.messagereply()
